package com.ruoyi.wordcloud.service.impl;

import java.io.IOException;
import java.util.List;

import Word_Frequency.FileLoader;
import Word_Frequency.TEST;
import jdk.nashorn.internal.objects.Global;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wordcloud.domain.WordcloudSlave;
import com.ruoyi.wordcloud.mapper.TextPMapper;
import com.ruoyi.wordcloud.domain.TextP;
import com.ruoyi.wordcloud.service.ITextPService;
import wordsCloud.WordCloudUtils;

/**
 * 展示词云图Service业务层处理
 * 
 * @author shiliu
 * @date 2024-04-26
 */
@Service
public class TextPServiceImpl implements ITextPService
{
    @Autowired
    private TextPMapper textPMapper;


    /**
     * 查询展示词云图
     * 
     * @param tpId 展示词云图主键
     * @return 展示词云图
     */
    @Override
    public TextP selectTextPByTpId(Long tpId)
    {
        return textPMapper.selectTextPByTpId(tpId);
    }

    /**
     * 查询展示词云图列表
     * 
     * @param textP 展示词云图
     * @return 展示词云图
     */
    @Override
    public List<TextP> selectTextPList(TextP textP)
    {
        return textPMapper.selectTextPList(textP);
    }

    /**
     * 新增展示词云图
     * 
     * @param textP 展示词云图
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTextP(TextP textP) throws IOException {

        //插入数据时直接完成具体业务

        //获取前端输入文本
        String tpContext = textP.getTpContext();

        //根据前端输入的文本生成词频文件
        TEST getWordFrequency = new TEST();
        getWordFrequency.Task(tpContext);

        //将词频文件转为String型存储
        FileLoader wordFrequencyToString = new FileLoader();
        String wordFrequencyFilePath = "ruoyi-hzhtest/src/main/resources/files/textrank.txt";
        String tpResults = wordFrequencyToString.Array(wordFrequencyFilePath);

        //根据词频文件生成词云图，将其路径存入数据库
        WordCloudUtils wordCloud = new WordCloudUtils();
        //生成的图片名称
        String imageName = wordCloud.createSimpleImage();

        textP.setTpResult(tpResults);
        textP.setType(imageName);


        int rows = textPMapper.insertTextP(textP);
        insertWordcloudSlave(textP);
        return rows;
    }

    /**
     * 修改展示词云图
     * 
     * @param textP 展示词云图
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTextP(TextP textP)
    {
        textPMapper.deleteWordcloudSlaveByTcTpid(textP.getTpId());

        //这是在点按更新按钮时进行的业务,和插入业务一样
        String tpContext = textP.getTpContext();
        //根据前端输入的文本生成词频文件
        TEST getWordFrequency = new TEST();
        try {
            getWordFrequency.Task(tpContext);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //将词频文件转为String型存储
        FileLoader wordFrequencyToString = new FileLoader();
        String wordFrequencyFilePath = "ruoyi-hzhtest/src/main/resources/files/textrank.txt";
        String tpResults = wordFrequencyToString.Array(wordFrequencyFilePath);
        //根据词频文件生成词云图，将其路径存入数据库
        WordCloudUtils wordCloud = new WordCloudUtils();
        //生成的图片名称
        String imageName = wordCloud.createSimpleImage();

        textP.setTpResult(tpResults);
        textP.setType(imageName);


        insertWordcloudSlave(textP);
        System.out.println(textP);
        return textPMapper.updateTextP(textP);
    }

    /**
     * 批量删除展示词云图
     * 
     * @param tpIds 需要删除的展示词云图主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTextPByTpIds(Long[] tpIds)
    {
        textPMapper.deleteWordcloudSlaveByTcTpids(tpIds);
        return textPMapper.deleteTextPByTpIds(tpIds);
    }

    /**
     * 删除展示词云图信息
     * 
     * @param tpId 展示词云图主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTextPByTpId(Long tpId)
    {
        textPMapper.deleteWordcloudSlaveByTcTpid(tpId);
        return textPMapper.deleteTextPByTpId(tpId);
    }

    /**
     * 新增词云图从表信息
     * 
     * @param textP 展示词云图对象
     */
    public void insertWordcloudSlave(TextP textP)
    {
        List<WordcloudSlave> wordcloudSlaveList = textP.getWordcloudSlaveList();
        Long tpId = textP.getTpId();
        if (StringUtils.isNotNull(wordcloudSlaveList))
        {
            List<WordcloudSlave> list = new ArrayList<WordcloudSlave>();
            for (WordcloudSlave wordcloudSlave : wordcloudSlaveList)
            {
                wordcloudSlave.setTcTpid(tpId);
                list.add(wordcloudSlave);
            }
            if (list.size() > 0)
            {
                textPMapper.batchWordcloudSlave(list);
            }
        }
    }
}
